﻿var rowCount = 41;
var colCount = 17;    
var row = 0;
var col = 0;
function doOnfocus(objID) {
    if (row != 0) {
        clearBG();
    }
    row = parseInt(objID.substring(5,objID.indexOf("_")));
    col = parseInt(objID.substr(objID.indexOf("_")+1, objID.length));    
    changeBG();
}

    
function changeBG() {

    for (i = 1; i <= row; i++) {
	    document.getElementById(i+"_"+col).style.backgroundColor="red";
    }

    for (i = 1; i <= col; i++) {
	    document.getElementById(row+"_"+i).style.backgroundColor="red";
    }

}

function clearBG() {

    for (i = 1; i <= row; i++) {
	    document.getElementById(i+"_"+col).style.backgroundColor="";
    }

    for (i = 1; i <= col; i++) {
	    document.getElementById(row+"_"+i).style.backgroundColor="";
    }

}

function doKeypress(e) {
    var key = window.event ? e.keyCode : e.which;

    // return->↓
    if (key == 13) {
        var r = row;
        var c = col;
        if (row == rowCount) {
            return false;
        } 
        r++;
        var newID = "input"+r+"_"+c;
        document.getElementById(newID).focus();
        return false;
    }
    
    if(key > 47 && key < 58 || key == 8 || key == 0)
    {
        return true;
    }
        return false;
}

function doKeydown(e) {
	var key = window.event ? e.keyCode : e.which;
	var r = row;
    var c = col;
/*
    var IsCtrl=window.event.ctrlKey;
    if(!IsCtrl){
        return;
    }    
*/
    if (key == 37){         //←
        if (col == 1) {
            return;
        }
        c--;
        
    }else if(key == 38){    //↑
        if (row == 1) {
            return;
        }    
        r--;
    }else if (key == 39){   //→
        if (col == colCount) {
            return;
        }    
        c++;    
        
    }else if (key == 40){   //↓
        if (row == rowCount) {
            return;
        } 
        r++;
    }
    
    var newID = "input"+r+"_"+c;
    document.getElementById(newID).focus();
}

function sumInputAmount() {
	var total = 0;
	var vals = document.getElementById('tabInputData').getElementsByTagName('input');
	for (var i=0; i<vals.length;i++) {
		if (vals[i].name.indexOf("row") > -1) {
			if (vals[i].value != "") {
				total += parseInt(vals[i].value);
			}
		}
	}
	return total;
}